<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目名" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目发起人" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入项目发起人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择项目阶段" clearable>
          <el-option v-for="dict in dict.type.pro_mainten_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customName">
        <el-input v-model="queryParams.customName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="取消人员" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入取消人员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mainten:mainten:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mainten:mainten:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mainten:mainten:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mainten:mainten:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维护编号" align="center" prop="maintenId" />
      <el-table-column label="项目名称" align="center" prop="projectName" width="150" />
      <!-- <el-table-column label="项目编号" align="center" prop="projectId" /> -->
      <el-table-column label="项目发起人" align="center" prop="userId" width="150" />
      <el-table-column label="计划开始时间" align="center" prop="startTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="正式开始日期" align="center" prop="endTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="expectedTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="正式结束时间" align="center" prop="formalTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.formalTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资金来源" align="center" prop="fundSoure" width="150" />
      <el-table-column label="资金预算" align="center" prop="budget" width="150">
        <template #default="scope">
          {{ formatNumber(scope.row.budget) }}
        </template>
      </el-table-column>
      <el-table-column label="预计总成本" align="center" prop="totalCost" width="150">
        <template #default="scope">
          {{ formatNumber(scope.row.totalCost) }}
        </template>
      </el-table-column>
      <el-table-column label="实际总成本" align="center" prop="realityCost" width="150">
        <template #default="scope">
          <div v-if="scope.row.realityCost > 1000">
            {{ formatNumber(scope.row.realityCost) }}
          </div>
          <div v-else>
            {{ scope.row.realityCost }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="status" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_mainten_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="项目合同和协议" align="center" prop="contract" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="viewContractFile(scope.row.contract)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="项目交付标准" align="center" prop="deliver" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="viewDeliverFile(scope.row.deliver)">查看</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="项目阶段" align="center" prop="phase" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_mainten_rank" :value="scope.row.phase" />
        </template>
      </el-table-column> -->
      <el-table-column label="客户名称" align="center" prop="customName" width="150" />
      <el-table-column label="取消时间" align="center" prop="cancelTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cancelTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="取消人员" align="center" prop="userName" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mainten:mainten:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mainten:mainten:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-close" @click="handleDel(scope.row)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改项目维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="projectName">
                <el-select :disabled="selDisabled" v-model="form.projectName" placeholder="请选择项目名称"
                  @change="updateSelectedUser">
                  <el-option v-for="dict in planList" :key="dict.projectName" :disabled="dict.disabled"
                    :label="dict.projectName" :value="dict.projectName"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目编号" prop="projectId">
                <el-input disabled v-model="form.projectId" placeholder="请输入项目编号" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-col :span="12">
            <el-form-item label="项目状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择项目状态" @change="onChangeTime">
                <el-option v-for="dict in dict.type.pro_mainten_status" :key="dict.value" :disabled="dict.disabled"
                  :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目发起人">
              <el-input disabled v-model="form.userId" placeholder="请输入项目发起人">
                <el-option v-for="dict in planList" :key="dict.duty" :label="dict.duty" :value="dict.duty"></el-option>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="计划开始时间" prop="startTime">
              <el-date-picker disabled clearable v-model="form.startTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择计划开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="正式开始日期" prop="endTime">
              <el-date-picker disabled v-model="form.endTime" type="datetime" placeholder="选择日期时间"
                default-time="12:00:00">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="计划结束时间" prop="expectedTime">
              <el-date-picker disabled clearable v-model="form.expectedTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择计划结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="customName">
              <el-input v-model="form.customName" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="资金来源" prop="fundSoure">
              <el-input v-model="form.fundSoure" placeholder="请输入资金来源" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资金预算" prop="budget">
              <el-input v-model="form.budget" placeholder="请输入资金预算" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="预计总成本" prop="totalCost">
              <el-input v-model="form.totalCost" placeholder="请输入预计总成本" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际总成本" prop="realityCost">
              <el-input v-model="form.realityCost" placeholder="请输入实际总成本" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="项目合同和协议" prop="contract">
              <file-upload v-model="form.contract" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目交付标准" prop="deliver">
              <file-upload v-model="form.deliver" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 项目完成对话框 -->
    <el-dialog :title="title" :visible.sync="openS" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="ruless" label-width="150px">

        <el-form-item label="结束时间" prop="cancelTime">
          <el-date-picker disabled v-model="form.cancelTime" type="datetime" placeholder="选择日期时间"
            default-time="12:00:00">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="取消人员" prop="userName">
          <el-select v-model="form.userName" placeholder="请选择取消人员" @change="updateSU">
            <el-option v-for="item in selUserList" :key="item.userName" :label="item.userName"
              :value="item.userName"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmitOk">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMainten, getMainten, delMainten, addMainten, updateMainten } from "@/api/system/mainten";
import { listUser, getUserProfile } from "@/api/system/user";
import { listPlan, updatePlan } from "@/api/system/plan";

export default {
  name: "Mainten",
  dicts: ['pro_mainten_status', 'pro_mainten_rank'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目维护表格数据
      maintenList: [],
      // 项目计划表格数据
      planList: [],
      // 项目取消人表格数据
      selUserList: [],
      // 项目选择状态
      selDisabled: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openS: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        userId: null,
        phase: null,
        customName: null,
        userName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '项目名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: "项目发起人不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "预计开始时间不能为空", trigger: "blur" }
        ],
        expectedTime: [
          { required: true, message: "预计结束时间不能为空", trigger: "blur" }
        ],
        fundSoure: [
          { required: true, message: "资金来源不能为空", trigger: "blur" },
          { pattern: /^[\u4e00-\u9fa5]+$/, message: '请输入有效的中文', trigger: ['blur', 'change'] }
        ],
        budget: [
          { required: true, message: "资金预算不能为空", trigger: "blur" },
          { pattern: /^[0-9]+$/, message: '请输入有效的数字', trigger: ['blur', 'change'] }
        ],
        totalCost: [
          { required: true, message: "预计总成本不能为空", trigger: "blur" },
          { pattern: /^[0-9]+$/, message: '请输入有效的数字', trigger: ['blur', 'change'] }
        ],
        realityCost: [
          { required: false, message: "实际总成本不能为空", trigger: "blur" },
          { pattern: /^[0-9]+$/, message: '请输入有效的数字', trigger: ['blur', 'change'] }
        ],
        status: [
          { required: true, message: "项目状态不能为空", trigger: "blur" }
        ],
        contract: [
          { required: true, message: "项目合同和协议不能为空", trigger: "blur" }
        ],
        deliver: [
          { required: true, message: "项目交付标准不能为空", trigger: "blur" }
        ],
        customName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" },
          { pattern: /^[\u4e00-\u9fa5]+$/, message: '请输入有效的中文', trigger: ['blur', 'change'] }
        ],
      },
      ruless: {
        userName: [
          { required: false, message: "取消人员不能为空", trigger: "blur" },
        ],
      },
      // 获取选中项目状态
      newStats: 0,
      // 已完成状态下的总成本-[类似缓存]
      selCost: 0,
    };
  },
  created() {
    this.getList();
    this.getListPlan();
    getUserProfile().then(response => {
      this.form.userId = response.data.userId;
    });
    listUser(this.queryParams).then(response => {
      this.selUserList = response.rows;
    });
  },
  methods: {
    // 获取开始时间
    onChangeTime(status) {
      if (status == 1 && this.form.endTime == null) {
        this.form.endTime = this.getTime();
      } else if (status == 2 && this.form.endTime == null) {
        this.form.endTime = this.getTime();
      }
      if (status == 4) {
        this.rules.realityCost[0].required = true;
        if (this.selCost != 0 ) {
          this.form.realityCost = this.selCost
        } else {
          this.form.realityCost = '';
        }
      } else {
        this.rules.realityCost[0].required = false;
        this.selCost = this.form.realityCost
        this.form.realityCost = '';
      }
    },
    // 获取当前时间
    getTime() {
      let date = new Date();
      const year = date.getFullYear();//获取年
      const month = date.getMonth() + 1;//获取月,注意时间是0-11，0代表1月份
      const dates = date.getDate();//获取日
      const hours = date.getHours();//获取小时
      const minute = date.getMinutes();//获取分钟
      const seconds = date.getSeconds();//获取秒钟
      return year + "-" + month + "-" + dates + " " + hours + ":" + minute + ":" + seconds;
    },
    // 获取项目取消人
    updateSU(selProName) {
      const selInfo = this.selUserList.find(user => user.userName === selProName);
      if (selInfo) {
        this.form.userName = selProName;
        this.form.cancelTime = this.getTime();
      } else {
        console.error(`未找到项目完成人为 "${selProName}" 的用户`);
        this.form.userName = '';
      }
    },
    // 转换金额
    formatNumber(num) {
      return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    },
    // 获取项目信息
    updateSelectedUser(selProName) {
      const selInfo = this.planList.find(plan => plan.projectName === selProName);
      if (selInfo) {
        if (selInfo.status == 0) {
          this.form.status = 0
        }
        if (selInfo.status == 1) {
          this.form.status = 2
        }
        if (selInfo.status == 2) {
          this.form.status = 4
        }
        if (selInfo.status == 3) {
          this.form.status = 9
        }
        if (this.form.status == 1 && this.form.endTime == null) {
          this.form.endTime = this.getTime();
        } else if (this.form.status == 2 && this.form.endTime == null) {
          this.form.endTime = this.getTime();
        }
        this.form.projectId = selInfo.projectId
        this.form.projectName = selInfo.projectName
        this.form.userId = selInfo.userId
        this.form.startTime = selInfo.planStartDate
        this.form.expectedTime = selInfo.planCompleteDate
      } else {
        console.error(`未找到 "${selProName}" 的项目计划`);
        this.form.projectId = '';
        this.form.projectName = '';
        this.form.userId = '';
        this.form.startTime = '';
        this.form.expectedTime = '';
      }
    },
    /** 查询项目计划列表 */
    getListPlan() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
      });
    },
    /** 查看文件 */
    viewContractFile(row) {
      let link = document.createElement('a');
      link.href = "http://127.0.0.1:8080" + row;
      link.click();
    },
    viewDeliverFile(row) {
      let link = document.createElement('a');
      link.href = "http://127.0.0.1:8080" + row;
      link.click();
    },
    /** 查询项目维护列表 */
    getList() {
      this.loading = true;
      listMainten(this.queryParams).then(response => {
        this.maintenList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openS = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        projectName: null,
        userId: null,
        startTime: null,
        endTime: null,
        expectedTime: null,
        formalTime: null,
        fundSoure: null,
        budget: null,
        totalCost: null,
        realityCost: null,
        status: null,
        contract: null,
        deliver: null,
        phase: null,
        customName: null,
        cancelTime: null,
        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.maintenId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 循环maintenList数组，判断是否存在以及是否已取消。
    processLists() {
      // 重置 planList 中所有项目的 disabled 属性  
      this.planList.forEach(item => {
        item.disabled = false; // 初始化为 false  
      });

      // 遍历 maintenList，并标记匹配的 plan 为 disabled  
      this.maintenList.forEach(maintenance => {
        const matchingPlan = this.planList.find(plan => plan.projectName === maintenance.projectName);
        if (matchingPlan) {
          matchingPlan.disabled = true;
        }
      });

      // 如果它的 status 也为 3，它仍然会被标记为 disabled（但这在逻辑上是正确的）  
      this.planList.forEach(plan => {
        if (plan.status === 3) {
          plan.disabled = true;
        }
      });

      // 项目状态取消禁止
      this.dict.type.pro_mainten_status.forEach(dictType => {
        if (dictType.value === '9' || dictType.value === 9) {
          dictType.disabled = true;
        }
      });

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目维护";
      this.selDisabled = false
      this.processLists();
      this.rules.realityCost[0].required = false;
      this.selCost = 0
    },
    // 获取完成时间
    DateDiffs(Date_end, Date_start) {
      let oDate1 = new Date(Date_end);
      let oDate2 = new Date(Date_start);
      // 计算两个日期之间的毫秒差异
      let diffInMilliseconds = Math.abs(oDate1 - oDate2);
      // 将毫秒差异转换为天数，包括小数部分
      let diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);
      return diffInDays.toFixed(1);
    },
    // 获取项目计划信息
    getPlanInfo() {
      const selInfo = this.planList.find(plan => plan.projectId === this.form.projectId);
      if (selInfo) {
        if (this.form.status == 1 && this.form.endTime != null) {
          selInfo.actualStartTime = this.form.endTime
          selInfo.status = 1
        }
        if (this.form.status == 4 && this.form.endTime != null && this.form.formalTime != null) {
          selInfo.actualStartTime = this.form.endTime
          selInfo.actualEndTime = this.form.formalTime
          selInfo.status = 2
          const daysDiff = this.DateDiffs(selInfo.actualEndTime, selInfo.actualStartTime);
          selInfo.workCompleteTime = daysDiff
        }
        if (this.form.status == 4 && this.form.formalTime != null) {
          selInfo.actualEndTime = this.form.formalTime
          selInfo.status = 2
          const daysDiff = this.DateDiffs(selInfo.actualEndTime, selInfo.actualStartTime);
          selInfo.workCompleteTime = daysDiff
        }
        if (this.form.status == 9) {
          selInfo.status = 3
        }
        updatePlan(selInfo).then(response => { });
      } else {
        console.error(`未找到 "${this.form.projectId}" 的项目计划`);
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const maintenId = row.maintenId || this.ids
      getMainten(maintenId).then(response => {
        this.form = response.data;
        this.newStats = this.form.status
        this.open = true;
        this.title = "修改项目维护";
      });

      // 项目状态取消禁止
      this.dict.type.pro_mainten_status.forEach(dictType => {
        if (dictType.value === '9' || dictType.value === 9) {
          dictType.disabled = true;
        }
      });

      this.selDisabled = true
      this.getRules(row)
      this.selCost = 0
    },
    // 获取表单验证
    getRules(row) {
      const selRules = this.maintenList.find(mainten => mainten.projectId === row.projectId);
      if (selRules) {
        if (selRules.status == 4) {
          this.rules.realityCost[0].required = true;
        } else {
          this.rules.realityCost[0].required = false;
        }
      } else {
        console.error(`未找到 "${this.form.projectId}" 的表单验证`);
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.ruless.userName[0].required = false;
      this.$refs["form"].validate(valid => {
        if (this.form.status == 1 && this.form.endTime == null) {
          this.form.endTime = this.getTime();
        } else if (this.form.status == 2 && this.form.endTime == null) {
          this.form.endTime = this.getTime();
        } else if (this.form.status == 4 && this.form.endTime == null && this.form.formalTime == null) {
          this.form.endTime = this.getTime();
          this.form.formalTime = this.getTime();
        } else if (this.form.status == 4 && this.form.formalTime == null) {
          this.form.formalTime = this.getTime();
        }
        if (valid) {
          if (this.form.maintenId != null) {
            if (this.newStats == 1) {
              if (this.form.status == 0) {
                this.$modal.msgError("已建设中的计划禁止改回未启动");
                return
              }
            } else if (this.newStats == 2) {
              if (this.form.status == 0) {
                this.$modal.msgError("已开发中的计划禁止改回未启动");
                return
              }
              if (this.form.status == 1) {
                this.$modal.msgError("已开发中的计划禁止改回建设中");
                return
              }
            } else if (this.newStats == 3) {
              if (this.form.status == 0) {
                this.$modal.msgError("已实施中的计划禁止改回未启动");
                return
              }
              if (this.form.status == 1) {
                this.$modal.msgError("已实施中的计划禁止改回建设中");
                return
              }
              if (this.form.status == 2) {
                this.$modal.msgError("已实施中的计划禁止改回开发中");
                return
              }
            } else if (this.newStats == 4) {
              if (this.form.status == 0) {
                this.$modal.msgError("已完成的计划禁止改回未启动");
                return
              }
              if (this.form.status == 1) {
                this.$modal.msgError("已完成的计划禁止改回建设中");
                return
              }
              if (this.form.status == 2) {
                this.$modal.msgError("已完成的计划禁止改回开发中");
                return
              }
              if (this.form.status == 3) {
                this.$modal.msgError("已完成的计划禁止改回实施中");
                return
              }
            } else if (this.newStats == 9) {
              this.$modal.msgError("已取消计划禁止修改");
              return
            }
            updateMainten(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            this.getPlanInfo();
          } else {
            addMainten(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            this.getPlanInfo();
          }
        }
      });
    },
    /** 取消按钮操作 */
    handleDel(row) {
      this.ruless.userName[0].required = true;
      this.reset();
      const maintenId = row.maintenId || this.ids
      getMainten(maintenId).then(response => {
        this.form = response.data;
        this.openS = true;
        this.title = "项目维护取消";
      });
    },
    /** 取消提交按钮 */
    handleSubmitOk() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.maintenId != null) {
            if (this.form.status == 9) {
              this.$modal.msgError("项目已取消");
              return
            } else {
              this.form.cancelTime = this.getTime();
              this.form.status = 9;
              updateMainten(this.form).then(response => {
                this.$modal.msgSuccess("取消成功");
                this.openS = false;
                this.getList();
              });
              this.getPlanInfo();
            }
          } else {
            this.$modal.msgSuccess("维护编号为空，取消失败。");
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const maintenIds = row.maintenId || this.ids;
      this.$modal.confirm('是否确认删除项目维护编号为"' + maintenIds + '"的数据项？').then(function () {
        return delMainten(maintenIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mainten/mainten/export', {
        ...this.queryParams
      }, `mainten_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>